package com.manner.groupon.service.user.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.PageResult;
import com.manner.groupon.common.ResultUtils;
import com.manner.groupon.mapper.UserMapper;
import com.manner.groupon.model.entity.User;
import com.manner.groupon.model.vo.LoginResultVo;
import com.manner.groupon.service.user.UserService;
import com.manner.groupon.utils.PhoneNumberValidator;
import com.manner.groupon.utils.WxMiniUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.manner.groupon.constant.CommonConstant.LOGIN_SIGN;
import static com.manner.groupon.constant.CommonConstant.USER_LOGIN_PREV;

/**
 * @author manner
 * @description 针对表[tb_user(用户表)]的数据库操作Service实现
 * @createDate 2024-10-13 14:26:26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private WxMiniUtil wxMiniUtil;

    @Resource
    private RedisTemplate<String, User> redisTemplate;

    @Resource
    private UserMapper userMapper;

    /**
     * @param code        登录手机号
     * @param invitePhone 邀请人手机号
     * @return BaseResponse<LoginResultVo < User>>
     * @description: 用户使用手机号登录
     * @author Manner
     * @date 2024/12/1 22:00
     */
    @Override
    public BaseResponse<LoginResultVo<User>> login(String code, String invitePhone) {
        //获取手机号
        String phone = wxMiniUtil.getPhone(code);
        //查询数据库
        User user = query().eq("phone", phone).one();
        //判断是否为空
        if (ObjUtil.isNull(user)) {
            //创建用户
            user = new User(phone, "微信用户");
            //为老客户邀请新的用户
            if (StrUtil.isNotBlank(invitePhone)) {
                //验证手机号格式
                if (PhoneNumberValidator.isValidPhoneNumber(invitePhone)) {
                    user.setInvitePhone(invitePhone);
                }
            }
            //新增
            save(user);
            //查询数据
            user = query().eq("phone", phone).one();
        }
        //准备令牌参数
        Map<String, Object> param = new HashMap<>();
        param.put("id", user.getId());
        param.put("phone", user.getPhone());
        //生成令牌
        String token = JWTUtil.createToken(param, LOGIN_SIGN.getBytes(StandardCharsets.UTF_8));
        //存入缓存
        redisTemplate.opsForValue().set(USER_LOGIN_PREV + token, user);
        //返回用户数据
        return ResultUtils.success(new LoginResultVo<>(user, token));
    }

    @Override
    public BaseResponse<PageResult<User>> query_search(Integer current, Integer pageSize) {
        //构建分页参数
        Page<User> page = new Page<>(current, pageSize);
        //查询
        Page<User> result = userMapper.selectPage(page, null);
        //返回数据
        return ResultUtils.success(new PageResult<>(result.getRecords(), result.getTotal()));
    }
}




